(ns acst.utils.core)


(defn handler [f & body]
  "usage: (handler func arg_name default_value)"
  (apply f (for [[a b] (partition 2 body)] (or a b))))

(defn conj-two
  [a b]
  (cond
    (= (first b) nil) a
    :else
    (recur (conj a (first b)) (rest b))))

(defmacro conj-all
  ([a b]
   `(conj-two ~a ~b))
  ([a b & c]
   `(conj-all (conj-two ~a ~b) ~@c)))

(defmacro deferror [map-name & error-map]
  (let [mx (apply #(reduce-kv (fn [m k v] (assoc m k [{:error v}])) {} %) error-map)]
    `(def ~map-name ~mx)))


(defmacro defresult [map-name & error-map]
  (let [mx (apply #(reduce-kv (fn [m k v] (assoc m k [{:result v}])) {} %) error-map)]
    `(def ~map-name ~mx)))

(defn parse-to-int [ & values]
  (into [] (map
             #(if (nil? %)
               nil
               (try (Integer. %)
                 (catch Exception _
                   -1))) values)))


